package stdio.costa.imoca.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import stdio.costa.imoca.notification.sms.SMSResult;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.core.util.MultivaluedMapImpl;

@Controller
@RequestMapping(value = "/email")
public class EmailController extends BaseController {

	@RequestMapping(value = "/send")
	public String send(final HttpServletRequest request,
			final HttpServletResponse response, final ModelMap result)
			throws Exception {

		String accessToken = request.getParameter("access_token");
		String to = request.getParameter("to");
		String bcc = request.getParameter("bcc");
		String cc = request.getParameter("cc");
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		

		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		WebResource service = client.resource(getBaseURI());

		MultivaluedMap<String, String> param = new MultivaluedMapImpl();
		param.add("to", to);
		param.add("bcc", bcc);
		param.add("cc", cc);
		param.add("title", title);
		param.add("content", content);
		param.add("access_token", accessToken);

		service.path("email").path("send")
				.queryParams(param).type(MediaType.APPLICATION_FORM_URLENCODED)
				.accept(MediaType.APPLICATION_JSON).post();

		return "null";
	}
}
